﻿Public Class PROVIDERSCLASS
    Dim sql As String = "SELECT CONCAT(`name`,' ',lname) AS `provider` FROM myfriendsdb.hospemp "
    Dim sql1 As String = "SELECT hospemp.empid , usrpolicy.upid , usrpolicy.empid , usrpolicy.prgid , usrpolicy.padd AS PADD  , usrpolicy.pdel AS PDEL , usrpolicy.pupd  AS PUPD , usrpolicy.pprt  AS PPRT , psee AS PSEE, prgcode ,prgname ,usreq , name ,lname , hospemp.CLINIC  FROM myfriendsdb.usrpolicy JOIN  masprg ON masprg.prgid  = usrpolicy.prgid   JOIN hospemp ON hospemp.empid = usrpolicy.empid "
    Dim dt As DataTable



    Dim dtusrprogrampolicies As DataTable
    Public Property useprogram As Boolean



    'เช็ค login User
    Dim checklogin_user As Boolean = False
    Public Property checklogin_user_ As Boolean

    Dim LOGINUSERMSG As String
    Public Property LOGINUSERMSG_ As String

    Dim policies_usr As Boolean = False
    Public Property policies_usr_ As Boolean

    Dim user As Boolean = False
    Public Property user_ As Boolean


  

    'ID EMP
    Dim txtusername_tag As String
    Public Property txtusername_tag_ As String

    Dim txtusername As String
    Public Property txtusername_ As String

    Dim btnlogout As Boolean
    Public Property btnlogout_ As Boolean

    Dim CHECKUSRFAIL As Boolean = False
    Public Property _CHECKUSRFAIL As Boolean

    Dim CHECKUSRFAILMSG As String
    Public Property _CHECKUSRFAILMSG As String

    Private PADD As Boolean
    Public Property PADD_ As Boolean

    Private PADDMSG As String
    Public Property PADDMSG_ As String

    Private PDEL As Boolean
    Public Property PDEL_ As Boolean
    Private PDELMSG As String
    Public Property PDELMSG_ As String



    Private PUPD As Boolean
    Public Property PUPD_ As Boolean
    Private PUPDMSG As String
    Public Property PUPDMSG_ As String



    Private PPRT As Boolean
    Public Property PPRT_ As Boolean
    Private PPRTMSG As String
    Public Property PPRTMSG_ As String


    Private PSEE As Boolean
    Public Property PSEE_ As Boolean
    Private PSEEMSG As String
    Public Property PSEEMSG_ As String

    Public Sub New()
        CHECKMSG()
    End Sub

    Public Function GETPROVIDER(USERSEQ As String) As String
        Dim db As CONDB = CONDB.NewConnection
        Dim dt As DataTable = New DataTable
        Dim sql2 As String = sql1 + "  WHERE hospemp.usreq ='" & USERSEQ & "';"
        dt = db.GetTable(sql2)
        db.Dispose()
        Return dt.Rows(0).Item("empid")
    End Function

    Public Function GETPROVIDERID(USERSEQ As String) As String
        Dim db As CONDB = CONDB.NewConnection
        Dim dt As DataTable = New DataTable
        Dim sql2 As String = sql1 + "  WHERE hospemp.usreq ='" & USERSEQ & "';"
        dt = db.GetTable(sql2)
        db.Dispose()
        Return dt.Rows(0).Item("provider")
    End Function
   
    Public Function GETDoctorZone(CLICNICID As String) As DataTable

        Dim dt As DataTable = New DataTable
        Dim db As CONDB = CONDB.NewConnection
        Dim sql As String = "SELECT p.empid AS 'PROVIDER',p.registerno,CONCAT(p.`name`,' ',p.lname) AS DOCTORNAME,COUNT(w.vn) AS Wait ,COUNT(s.Vn) AS Complete ,p.f_login  FROM  myfriendsdb.hospemp AS p   LEFT  JOIN     "
        sql += "(SELECT * FROM myfriendsdb.frnclinic WHERE clinic = " + Convert.ToString(CLICNICID).Trim + ") AS c  ON c.doctor = p.empid "
        sql += "LEFT JOIN (SELECT * FROM myfriendsdb.frnclinic WHERE clinic = " + Convert.ToString(CLICNICID).Trim + " AND cstatus IN(1,2,3)) AS w ON c.vn = w.vn "
        sql += "LEFT JOIN (SELECT * FROM myfriendsdb.frnclinic WHERE clinic = " + Convert.ToString(CLICNICID).Trim + " AND cstatus IN(4,5,6)) AS s ON c.vn = s.vn "
        sql += " "
        sql += "WHERE    p.clinic =  " + Convert.ToString(CLICNICID).Trim + " GROUP BY c.doctor"

        dt = db.GetTable(sql)
        Return dt
    End Function

    ' function GETHOSEM มีสองแบบ แก้บัคเบื้องต้น ส่ง IDUSER REQUEST กับ ชื่อ FORM.TAG ส่งมาเพื่อ ใช้เช็คสิทธิ์ในการเข้าใช้งาน

    Public Sub LOGOUT(PRGNAME As String)
        btnlogout_ = False
        txtusername_ = Nothing
        txtusername_tag_ = Nothing
        checklogin_user_ = False
        LOADDEFAULTPOLICIES(PRGNAME)
    End Sub
    Public Sub LOADDEFAULTPOLICIES(PRGNAME As String)
        Dim sql2 As String = sql1 + "WHERE prgcode = '" & PRGNAME & "' AND  hospemp.empid = 1 ;"
        Dim db As CONDB = CONDB.NewConnection
        dt = db.GetTable(sql2)
        If dt.Rows.Count <= 0 Then
            PADD_ = False
            PDEL_ = False
            PUPD_ = False
            PPRT_ = False
            PSEE_ = False
            _CHECKUSRFAIL = True
            ' _objloginusr._btnlogout = True


        Else
            PADD_ = Convert.ToBoolean(dt.Rows(0)("PADD"))
            PDEL_ = Convert.ToBoolean(dt.Rows(0)("PDEL"))
            PUPD_ = Convert.ToBoolean(dt.Rows(0)("PUPD"))
            PPRT_ = Convert.ToBoolean(dt.Rows(0)("PPRT"))
            PSEE_ = Convert.ToBoolean(dt.Rows(0)("PSEE"))
            txtusername_ = dt.Rows(0)("NAME") & " " & dt.Rows(0)("LNAME")
            txtusername_tag_ = dt.Rows(0)("empid")
        End If
    End Sub

    Public Function GETCHECKVIEWPRG(PRGNAME As String) As Boolean
        For i As Integer = 0 To dtusrprogrampolicies.Rows.Count - 1
            If dtusrprogrampolicies.Rows(i)("PRGCODE").ToString = PRGNAME Then
                If Convert.ToBoolean(dtusrprogrampolicies.Rows(0)("PSEE")) = False Then
                    PSEEMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.VIEWMSG_F
                    Return False
                    Exit Function
                Else
                    Return True
                    Exit Function
                End If


            End If
        Next
        Return False
    End Function

    Public Sub LOADPOLICIES(IDUSER As Integer)
        Dim db As CONDB = CONDB.NewConnection
        Dim sql As String
        sql = "SELECT psee ,prgcode  FROM   ( SELECT empid,prgid , psee FROM  myfriendsdb.usrpolicy WHERE empid =" & IDUSER & ") as usrpolicy   JOIN  myfriendsdb.masprg ON usrpolicy.prgid =   masprg.prgid "
        dtusrprogrampolicies = db.GetTable(sql)
        db.Dispose()

    End Sub
    Public Sub GETHOSEM(IDUSER As String, PRGNAME As String)

        Dim db As CONDB = CONDB.NewConnection
        Dim sql2 As String = sql1 + " WHERE  usreq = '" & IDUSER & "' AND  prgcode ='" & PRGNAME & "';"
        dt = db.GetTable(sql2)
        If dt.Rows.Count <= 0 Then
            Dim sqlcheck As String = sql1 + " WHERE usreq = '" & IDUSER & "' ; "
            Dim dtcheckuser As New DataTable
            Dim db1 As CONDB = CONDB.NewConnection
            dtcheckuser = db1.GetTable(sqlcheck)
            If dtcheckuser.Rows.Count <= 0 Then
                user_ = False
            Else
                user_ = True
            End If
            PADD_ = False
            PDEL_ = False
            PUPD_ = False
            PPRT_ = False
            PSEE_ = False
            _CHECKUSRFAIL = True
            ' _objloginusr._btnlogout = True
            btnlogout_ = False
            txtusername_ = Nothing
            txtusername_tag_ = Nothing
            policies_usr_ = False
            checklogin_user_ = False
            LOADDEFAULTPOLICIES(PRGNAME)
        Else
            user_ = True
            PADD_ = Convert.ToBoolean(dt.Rows(0)("PADD"))
            PDEL_ = Convert.ToBoolean(dt.Rows(0)("PDEL"))
            PUPD_ = Convert.ToBoolean(dt.Rows(0)("PUPD"))
            PPRT_ = Convert.ToBoolean(dt.Rows(0)("PPRT"))
            PSEE_ = Convert.ToBoolean(dt.Rows(0)("PSEE"))
            _CHECKUSRFAIL = False
            btnlogout_ = True
            policies_usr_ = True
            checklogin_user_ = True
            txtusername_ = dt.Rows(0)("name") & " " & dt.Rows(0)("lname")
            txtusername_tag_ = dt.Rows(0)("empid")
            LOADPOLICIES(dt.Rows(0)("empid"))
        End If
        'เรียก function ใส่ข้อความ MSG
        CHECKMSG()
        db.Dispose()
        '   Return objloginusr
    End Sub


    'นำข้อความาใส่ ใน MSG ไว้แสดงผล สิทธิ์ การเข้าใช้งาน

    Public Sub CHECKMSG()
        If PADD_ = False Then
            PADDMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.ADDMSG_F
        ElseIf PADD_ = True Then
            PADDMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.ADDMSG_T
        End If

        If PDEL_ = False Then
            PDELMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.DELMSG_F
        ElseIf PDEL_ = True Then
            PDELMSG = ENVIRONMENTCLASS.USERUSEEVENT.DELMSG_T
        End If

        If PUPD_ = False Then
            PUPDMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.EDITMSG_F
        ElseIf PUPD_ = True Then
            PUPDMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.EDITMSG_T
        End If


        If PPRT_ = False Then
            PPRTMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.PRINTMSG_F
        ElseIf PPRT_ = True Then
            PPRTMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.PRINTMSG_T
        End If

        If PSEE_ = False Then
            PSEEMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.VIEWMSG_F
        ElseIf PSEE_ = True Then
            PSEEMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.VIEWMSG_T
        End If

        If _CHECKUSRFAIL = False Then
            _CHECKUSRFAILMSG = ""
        ElseIf _CHECKUSRFAIL = True Then

            If user_ = True Then
                If policies_usr_ = False Then
                    _CHECKUSRFAILMSG = ENVIRONMENTCLASS.USERUSEEVENT.USERFAILMSG_POLICIES_TH

                Else
                    _CHECKUSRFAILMSG = ENVIRONMENTCLASS.USERUSEEVENT.USERFAILMSG_TH_F
                End If
            ElseIf user_ = False Then
                _CHECKUSRFAILMSG = ENVIRONMENTCLASS.USERUSEEVENT.USERFAILMSG_NOTREGIST_TH
            End If

        End If
        If checklogin_user_ = False Then
            LOGINUSERMSG_ = ENVIRONMENTCLASS.USERUSEEVENT.USERCANNOT_LOGIN_TH
        End If

    End Sub

End Class
